From d36e9ea5bc55d189735ef9f55433ffff5b52550a Mon Sep 17 00:00:00 2001 From: Jesse Keating Date: Wed, 23 Jun 2010 21:53:30 -0700 Subject: Move import_srpm out of the class This is so that we can use it before we have a full fledged module to deal with, like creating something local to play with. --- src/fedpkg.py | 16 ++--- src/pyfedpkg/__init__.py | 160 +++++++++++++++++++++++------------------------ 2 files changed, 87 insertions(+), 89 deletions(-) (limited to 'src') diff --git a/src/fedpkg.py b/src/fedpkg.py index 1c1f0af..6e12776 100755 --- a/src/fedpkg.py +++ b/src/fedpkg.py @@ -441,17 +441,17 @@ def import_srpm(args): if not args.create: try: mymodule = pyfedpkg.PackageModule(args.path) - if not mymodule.import_srpm(args.srpm): - print("New content staged and new sources uploaded.") - print("Review with: git diff --cached") - print("Commit if happy or revert with: git reset --hard HEAD") - return - else: - log.error("Unable to import srpm") - sys.exit(1) + uploadfiles = pyfedpkg.import_srpm(mymodule.repo, args.srpm) except pyfedpkg.FedpkgError, e: log.error('Could import srpm: %s' % e) sys.exit(1) + # replace this system call with a proper diff target when it is + # readys + os.system('GIT_PAGER='' git diff --cached') + print('--------------------------------------------') + print("New content staged and new sources uploaded.") + print("Commit if happy or revert with: git reset --hard HEAD") + return def install(args): arch = None diff --git a/src/pyfedpkg/__init__.py b/src/pyfedpkg/__init__.py index bf9481f..a42bc51 100644 --- a/src/pyfedpkg/__init__.py +++ b/src/pyfedpkg/__init__.py @@ -251,6 +251,85 @@ def get_latest_commit(module): # Return the hash sum return output.split()[0] +def import_srpm(repo, srpm, path=os.getcwd()): + """Import the contents of an srpm into a repo. + + repo: a git repo object to use for the import + + srpm: File to import contents from + + path: optional path to work in, defaults to cwd. + + This function will add/remove content to match the srpm, + + upload new files to the lookaside, and stage the changes. + + Returns a list of files to upload. + + """ + + # see if the srpm even exists + srpm = os.path.abspath(srpm) + if not os.path.exists(srpm): + raise FedpkgError('File not found.') + # bail if we're dirty + if repo.is_dirty(): + raise FedpkgError('There are uncommitted changes in your repo') + # Get the details of the srpm + name, files, uploadfiles = _srpmdetails(srpm) + + # Need a way to make sure the srpm name matches the repo some how. + + # Get a list of files we're currently tracking + ourfiles = repo.git.ls_files().split() + # Trim out sources and .gitignore + try: + ourfiles.remove('.gitignore') + ourfiles.remove('sources') + except ValueError: + pass + try: + ourfiles.remove('sources') + except ValueError: + pass + + # Things work better if we're in our module directory + oldpath = os.getcwd() + os.chdir(path) + + # Look through our files and if it isn't in the new files, remove it. + for file in ourfiles: + if file not in files: + log.info("Removing no longer used file: %s" % file) + rv = repo.index.remove([file]) + os.remove(file) + + # Extract new files + cmd = ['rpm2cpio', srpm] + # We have to force cpio to copy out (u) because git messes with + # timestamps + cmd2 = ['cpio', '-iud', '--quiet'] + + rpmcall = subprocess.Popen(cmd, stdout=subprocess.PIPE) + cpiocall = subprocess.Popen(cmd2, stdin=rpmcall.stdout) + output, err = cpiocall.communicate() + if output: + log.debug(output) + if err: + os.chdir(oldpath) + raise FedpkgError("Got an error from rpm2cpio: %s" % err) + + # And finally add all the files we know about (and our stock files) + for file in ('.gitignore', 'sources'): + if not os.path.exists(file): + # Create the file + open(file, 'w').close() + files.append(file) + rv = repo.index.add(files) + # Return to the caller and let them take it from there. + os.chdir(oldpath) + return(uploadfiles) + def new(path=os.getcwd()): """Return changes in a repo since the last tag""" @@ -548,87 +627,6 @@ class PackageModule: self.kojisession.uploadWrapper(file, path, callback = callback) return - def import_srpm(self, srpm): - """Import the contents of an srpm into a repo. - - srpm: File to import contents from - - This function will add/remove content to match the srpm, - - upload new files to the lookaside, and stage the changes. - - Returns nothing or raises. - - """ - - - # see if the srpm even exists - srpm = os.path.abspath(srpm) - if not os.path.exists(srpm): - raise FedpkgError('File not found.') - # bail if we're dirty - if self.repo.is_dirty(): - raise FedpkgError('There are uncommitted changes in your repo') - # Get the details of the srpm - name, files, uploadfiles = _srpmdetails(srpm) - - # See if our module matches the srpm we're trying to imoprt - if name != self.module: - raise FedpkgError('Srpm does not match module') - - # Get a list of files we're currently tracking - ourfiles = self.repo.git.ls_files().split() - # Trim out sources and .gitignore - try: - ourfiles.remove('.gitignore') - ourfiles.remove('sources') - except ValueError: - pass - try: - ourfiles.remove('sources') - except ValueError: - pass - - # Things work better if we're in our module directory - oldpath = os.getcwd() - os.chdir(self.path) - - # Look through our files and if it isn't in the new files, remove it. - for file in ourfiles: - if file not in files: - log.info("Removing no longer used file: %s" % file) - rv = self.repo.index.remove([file]) - os.remove(file) - - # Extract new files - cmd = ['rpm2cpio', srpm] - # We have to force cpio to copy out (u) because git messes with - # timestamps - cmd2 = ['cpio', '-iud', '--quiet'] - - rpmcall = subprocess.Popen(cmd, stdout=subprocess.PIPE) - cpiocall = subprocess.Popen(cmd2, stdin=rpmcall.stdout) - output, err = cpiocall.communicate() - if output: - log.debug(output) - if err: - os.chdir(oldpath) - raise FedpkgError("Got an error from rpm2cpio: %s" % err) - - # now process the upload files - if uploadfiles: - self.new_sources(uploadfiles) - # And finally add all the files we know about (and our stock files) - for file in ('.gitignore', 'sources'): - if not os.path.exists(file): - # Create the file - open(file, 'w').close() - files.append(file) - rv = self.repo.index.add(files) - # Return to the caller and let them take it from there. - os.chdir(oldpath) - return - def init_koji(self, user, kojiconfig=None, url=None): """Initiate a koji session. Available options are: -- cgit