From 29796aaa021934669e90fe0504cbee94540134b3 Mon Sep 17 00:00:00 2001
From: dsilakov <dsilakov@odin.com>
Date: Sep 29 2015 09:03:10 +0000
Subject: [PATCH 1/4] Recognize binary files with .oxt and .xpi extensions


---

diff --git a/src/pyrpkg/__init__.py b/src/pyrpkg/__init__.py
index b9f16be..cb6549f 100644
--- a/src/pyrpkg/__init__.py
+++ b/src/pyrpkg/__init__.py
@@ -69,7 +69,8 @@ class Commands(object):
     # This shouldn't change... often
     UPLOADEXTS = ['tar', 'gz', 'bz2', 'lzma', 'xz', 'Z', 'zip', 'tff',
                   'bin', 'tbz', 'tbz2', 'tgz', 'tlz', 'txz', 'pdf', 'rpm',
-                  'jar', 'war', 'db', 'cpio', 'jisp', 'egg', 'gem', 'spkg']
+                  'jar', 'war', 'db', 'cpio', 'jisp', 'egg', 'gem', 'spkg',
+                  'oxt', 'xpi']
 
     def __init__(self, path, lookaside, lookasidehash, lookaside_cgi,
                  gitbaseurl, anongiturl, branchre, kojiconfig,
@@ -2466,3 +2467,4 @@ class Commands(object):
                           ' %s file', self.osbs_config_filename)
         else:
             self.log.info('Nothing to be done')
+

From c97709fed8e76875910c49ba2343733cbd3f97df Mon Sep 17 00:00:00 2001
From: Denis Silakov <dsilakov@gmail.com>
Date: Oct 20 2015 09:32:42 +0000
Subject: [PATCH 2/4] If no args specified for new-sources or upload cmds, upload all binary sources found in project folder


---

diff --git a/src/pyrpkg/__init__.py b/src/pyrpkg/__init__.py
index cb6549f..a488148 100644
--- a/src/pyrpkg/__init__.py
+++ b/src/pyrpkg/__init__.py
@@ -2195,6 +2195,40 @@ class Commands(object):
                            config_dir)
             self._cleanup_tmp_dir(config_dir)
 
+    def choose_uploads(self):
+        """
+        Choose files from the project folder that should be uploaded
+        to lookaside cache - parse Source fields from the spec file
+        and choose existing binary files from that set
+        """
+        ts = rpm.TransactionSet()
+        try:
+            sources_all = ts.parseSpec(os.path.join(self.path, self.spec)).sources
+        except:
+            raise rpkgError("Error parsing spec file %s" % self.spec)
+
+        upload_files = []
+
+        # A flag distinguishing "Source" from "Patch"
+        src_flag = 1
+
+        for src in sources_all:
+            name = os.path.basename(src[0])
+            # Process only source file that exist in the project folder
+            if not os.path.isfile(os.path.join(self.path, name)):
+                continue
+            if not src[2] & src_flag:
+                continue
+            # Upload file if its extension is in UPLOADEXTS
+            if not name.rsplit('.')[-1] in self.UPLOADEXTS:
+                continue
+            upload_files.append(name)
+
+        if not upload_files:
+            raise rpkgError("Didn't find any files to be uploaded!")
+
+        return upload_files
+
     def upload(self, files, replace=False):
         """Upload source file(s) in the lookaside cache
 
diff --git a/src/pyrpkg/cli.py b/src/pyrpkg/cli.py
index b0325c1..3f6bbee 100755
--- a/src/pyrpkg/cli.py
+++ b/src/pyrpkg/cli.py
@@ -613,8 +613,10 @@ defined, packages will be built sequentially.""" % {'name': self.name})
             description='This will upload new source files to the lookaside '
                         'cache and remove any existing ones. The "sources" '
                         'and .gitignore files will be updated with the new '
-                        'uploaded file(s).')
-        self.new_sources_parser.add_argument('files', nargs='+')
+                        'uploaded file(s). If no arguments are provided, all '
+                        'binary files from the project folder that are '
+                        'mentioned in the spec file will be uploaded.')
+        self.new_sources_parser.add_argument('files', nargs='*')
         self.new_sources_parser.set_defaults(
             command=self.new_sources, replace=True)
 
@@ -780,7 +782,10 @@ defined, packages will be built sequentially.""" % {'name': self.name})
             conflict_handler='resolve', help='Upload source files',
             description='This command will add a new source archive to the '
                         'lookaside cache. The sources and .gitignore file '
-                        'will be updated with the new file(s).')
+                        'will be updated with the new file(s). If no '
+                        'arguments are provided, all binary files from the '
+                        'project folder that are mentioned in the spec file '
+                        'will be uploaded.')
         upload_parser.set_defaults(command=self.new_sources, replace=False)
 
     def register_verify_files(self):
@@ -1156,11 +1161,15 @@ defined, packages will be built sequentially.""" % {'name': self.name})
         print(self.cmd.new())
 
     def new_sources(self):
-        # Check to see if the files passed exist
-        for file in self.args.files:
-            if not os.path.isfile(file):
-                raise Exception('Path does not exist or is '
-                                'not a file: %s' % file)
+        # If no files were passed, upload all binaries mentioned in spec
+        if not self.args.files:
+            self.args.files = self.cmd.choose_uploads()
+        else:
+            # Check to see if the files passed exist
+            for file in self.args.files:
+                if not os.path.isfile(file):
+                    raise Exception('Path does not exist or is '
+                                    'not a file: %s' % file)
         self.cmd.upload(self.args.files, replace=self.args.replace)
         self.log.info("Source upload succeeded. Don't forget to commit the "
                       "sources file")

From 29fe536e65f0766c18aea0405893c4aa5a78133c Mon Sep 17 00:00:00 2001
From: Denis Silakov <dsilakov@gmail.com>
Date: Nov 03 2016 16:55:58 +0000
Subject: [PATCH 3/4] Added a test to check new choose_upload function


---

diff --git a/test/commands/test_upload.py b/test/commands/test_upload.py
new file mode 100644
index 0000000..4f63f92
--- /dev/null
+++ b/test/commands/test_upload.py
@@ -0,0 +1,50 @@
+import os
+import shutil
+import tempfile
+
+from . import CommandTestCase
+
+
+class CommandUploadTestCase(CommandTestCase):
+    def test_choose_upload(self):
+        self.make_new_git(self.module)
+
+        import pyrpkg
+        cmd = pyrpkg.Commands(self.path, self.lookaside, self.lookasidehash,
+                              self.lookaside_cgi, self.gitbaseurl,
+                              self.anongiturl, self.branchre, self.kojiconfig,
+                              self.build_client, self.user, self.dist,
+                              self.target, self.quiet)
+        cmd.clone(self.module, anon=True)
+
+        moduledir = os.path.join(self.path, self.module)
+        self.assertTrue(os.path.isdir(os.path.join(moduledir, '.git')))
+        cmd.path = moduledir
+
+        # Create a dummy (but valid) spec file which has two sources:
+        # * test-1.0.tar.gz (in the form of '%{name}-%{version}')
+        # * valid_source.tar.gz
+        test_spec = os.path.join(moduledir, 'test.spec')
+        f = open(test_spec, "w")
+        f.write("Name: test\nVersion: 1.0\nRelease: 1\nSummary: test\n")
+        f.write("License: test\nGroup: test\nURL: test\n")
+        f.write("Source0: %{name}-%{version}.tar.gz\n")
+        f.write("Source1: valid_source.tar.gz\n")
+        f.write("%description\ntest\n%prep\n%setup -q\n%build\n\n%files\n")
+        f.close()
+        owd = os.getcwd()
+        os.chdir(moduledir)
+        os.system("git add test.spec")
+        # Create 3 tar balls: test-1.0, valid_source and wrong_file
+        os.system("tar cf test-1.0.tar.gz test.spec")
+        os.system("tar cf valid_source.tar.gz test.spec")
+        os.system("tar cf wrong_file.tar.gz test.spec")
+        cmd.commit(message="Test_commit")
+        # We expect that rpkg will pick up only test-1.0.tar.gz and
+        # valid_source.tar.gz for upload
+        files = cmd.choose_uploads()
+        self.assertTrue("test-1.0.tar.gz" in files)
+        self.assertTrue("valid_source.tar.gz" in files)
+        self.assertFalse("wrong_file.tar.gz" in files)
+        os.chdir(owd)
+

From 721eacb653941a3d6584f9b324e95f1204ad79a8 Mon Sep 17 00:00:00 2001
From: Denis Silakov <dsilakov@gmail.com>
Date: Nov 08 2016 14:45:24 +0000
Subject: [PATCH 4/4] Catch exceptions in getcwd during test_upload check


---

diff --git a/test/commands/test_upload.py b/test/commands/test_upload.py
index 4f63f92..8a39344 100644
--- a/test/commands/test_upload.py
+++ b/test/commands/test_upload.py
@@ -32,7 +32,10 @@ class CommandUploadTestCase(CommandTestCase):
         f.write("Source1: valid_source.tar.gz\n")
         f.write("%description\ntest\n%prep\n%setup -q\n%build\n\n%files\n")
         f.close()
-        owd = os.getcwd()
+        try:
+            owd = os.getcwd()
+        except:
+            owd = "."
         os.chdir(moduledir)
         os.system("git add test.spec")
         # Create 3 tar balls: test-1.0, valid_source and wrong_file

