From d317f86441fb55861d6cb4094ca7955662c0f9e5 Mon Sep 17 00:00:00 2001 From: Siyuan Lou Date: Jul 02 2024 04:02:03 +0000 Subject: add argument destdir to R2spec to specify the generated path of the spec file --- diff --git a/r2spec/r2spec_obj.py b/r2spec/r2spec_obj.py index 9f0f076..3799037 100644 --- a/r2spec/r2spec_obj.py +++ b/r2spec/r2spec_obj.py @@ -85,6 +85,8 @@ def setup_parser(prog): help='Give more info about what is going on.') parser.add_argument('--debug', action='store_true', help='Output bunches of debugging info.') + parser.add_argument('--destdir', + help='Custom path to save the generated spec file.') if prog == 'R2rpm': setup_r2rpm_parser(parser) return parser @@ -284,11 +286,13 @@ class R2spec(object): pack.get_description() pack.determine_arch() + destdir = args.destdir if hasattr(args, 'destdir') else None + spec = Spec(settings, pack, no_check=args.no_check, with_deps=args.with_deps) spec.fill_spec_info() spec.get_template() - spec.write_spec(True) + spec.write_spec(True, destdir=destdir) return pack diff --git a/r2spec/spec.py b/r2spec/spec.py index 859e742..c6ffdcb 100644 --- a/r2spec/spec.py +++ b/r2spec/spec.py @@ -228,12 +228,15 @@ class Spec: self.log.debug('ERROR: %s', err) raise R2specError('Cannot read the file %s' % (err, )) - def get_specfile(self): + def get_specfile(self, destdir=None): """ Return the path to the spec file. """ - specdir = get_rpm_tag('_specdir') - specname = 'R-%s.spec' % self.package.name - return '%s/%s' % (specdir, specname) + if destdir: + return os.path.join(destdir, f'R-{self.package.name}.spec') + else: + specdir = get_rpm_tag('_specdir') + specname = 'R-%s.spec' % self.package.name + return '%s/%s' % (specdir, specname) def read_specfile(self): """ Read the specfile present in the spec directory. @@ -249,10 +252,10 @@ class Spec: self.log.info('Cannot read the file %s', specfile) self.log.debug('ERROR: %s', err) - def write_spec(self, verbose=False): + def write_spec(self, verbose=False, destdir=None): """ Write down the spec to the spec directory as returned by rpm. """ - specfile = self.get_specfile() + specfile = self.get_specfile(destdir) self.log.info('Writing file %s', specfile) try: stream = open(specfile, 'w')