summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSam Thursfield <sam.thursfield@codethink.co.uk>2014-09-11 16:26:59 +0100
committerSam Thursfield <sam.thursfield@codethink.co.uk>2014-09-11 16:26:59 +0100
commit299fb466b83650476377c2e6d24218a62a3e99f2 (patch)
tree8fd8027242da67dcd742c4497ea2b81eeccc049f
parent3126df70878b69ed883c9e3f128b0baab75f61c3 (diff)
downloadmorph-sam/ruby-integration-old-2.tar.gz
import: Add base class for Python importerssam/ruby-integration-old-2
-rw-r--r--import/importer_base.py64
-rwxr-xr-ximport/rubygems.to_lorry72
2 files changed, 85 insertions, 51 deletions
diff --git a/import/importer_base.py b/import/importer_base.py
new file mode 100644
index 00000000..1e785d4e
--- /dev/null
+++ b/import/importer_base.py
@@ -0,0 +1,64 @@
+# Base class for import tools written in Python.
+#
+# Copyright (C) 2014 Codethink Limited
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; version 2 of the License.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+
+import cliapp
+
+import logging
+import os
+
+
+class ImportExtension(cliapp.Application):
+ '''A base class for import extensions.
+
+ A subclass should subclass this class, and add a ``process_args`` method.
+
+ Note that it is not necessary to subclass this class for import extensions.
+ This class is here just to collect common code.
+
+ '''
+
+ def setup_logging(self):
+ # For old cliapp
+ self.setup_logging_to_file()
+
+ def setup_logging_to_file(self):
+ '''Direct all logging output to MORPH_LOG_FD, if set.
+
+ This file descriptor is read by Morph and written into its own log
+ file.
+
+ This overrides cliapp's usual configurable logging setup.
+
+ '''
+ # For new cliapp
+ log_write_fd = int(os.environ.get('MORPH_LOG_FD', 0))
+
+ if log_write_fd == 0:
+ return
+
+ formatter = logging.Formatter('%(message)s')
+
+ handler = logging.StreamHandler(os.fdopen(log_write_fd, 'w'))
+ handler.setFormatter(formatter)
+
+ logger = logging.getLogger()
+ logger.addHandler(handler)
+ logger.setLevel(logging.DEBUG)
+
+ def process_args(self, args):
+ raise NotImplementedError()
diff --git a/import/rubygems.to_lorry b/import/rubygems.to_lorry
index da0fdc05..65f5d260 100755
--- a/import/rubygems.to_lorry
+++ b/import/rubygems.to_lorry
@@ -28,10 +28,13 @@ import os
import sys
import urlparse
+import importer_base
+
class GenerateLorryException(Exception):
pass
+
class RubyGemsWebServiceClient(object):
def __init__(self):
# Save hammering the rubygems.org API: 'requests' API calls are
@@ -59,8 +62,10 @@ class RubyGemsWebServiceClient(object):
return info
-class RubyGemLorryGenerator(object):
+class RubyGemLorryGenerator(importer_base.ImportExtension):
def __init__(self):
+ super(importer_base.ImportExtension, self).__init__()
+
with open('rubygems.yaml', 'r') as f:
local_data = yaml.load(f.read())
@@ -69,6 +74,16 @@ class RubyGemLorryGenerator(object):
logging.debug(
"Loaded %i known source URIs from local metadata.", len(self.known_source_uris))
+ def process_args(self, args):
+ if len(args) != 1:
+ raise cliapp.AppException(
+ 'Please call me with the name of a RubyGem as an argument.\n')
+
+ gem_name = args[0]
+
+ lorry = self.generate_lorry_for_gem(gem_name)
+ self.write_lorry(sys.stdout, lorry)
+
def find_upstream_repo_for_gem(self, gem_name, gem_info):
source_code_uri = gem_info['source_code_uri']
@@ -138,56 +153,11 @@ class RubyGemLorryGenerator(object):
return lorry
-
-def setup_logging():
- '''Direct all logging output to MORPH_LOG_FD, if set.
-
- This file descriptor is read by Morph and written into its own log
- file.
-
- This overrides cliapp's usual configurable logging setup.
-
- '''
- log_write_fd = int(os.environ.get('MORPH_LOG_FD', 0))
-
- if log_write_fd == 0:
- return
-
- formatter = logging.Formatter('%(message)s')
-
- handler = logging.StreamHandler(os.fdopen(log_write_fd, 'w'))
- handler.setFormatter(formatter)
-
- logger = logging.getLogger()
- logger.addHandler(handler)
- logger.setLevel(logging.DEBUG)
-
-
-def write_lorry(stream, lorry):
- json.dump(lorry, stream, indent=4)
- stream.write('\n')
-
-
-def main():
- if len(sys.argv) != 2:
- sys.stderr.write(
- 'Please call me with the name of a RubyGem as an argument.\n')
- sys.exit(1)
-
- setup_logging()
-
- gem_name = sys.argv[1]
-
- lorry_generator = RubyGemLorryGenerator()
- lorry = lorry_generator.generate_lorry_for_gem(gem_name)
-
- write_lorry(sys.stdout, lorry)
+ def write_lorry(self, stream, lorry):
+ json.dump(lorry, stream, indent=4)
+ # Needed so the morphlib.extensions code will pick up the last line.
+ stream.write('\n')
if __name__ == '__main__':
- try:
- main()
- except GenerateLorryException as e:
- sys.stderr.write(e.message)
- sys.stderr.write('\n')
- sys.exit(1)
+ RubyGemLorryGenerator().run()