Commits

Seraphim Mellos  committed b8aac8f

Added -t/-s flags on push for source/translations selection

  • Participants
  • Parent commits 886ee94

Comments (0)

Files changed (2)

File txclib/commands.py

     parser.add_option("--skip", action="store_true", dest="skip_errors",
         default=False, help="Don't stop on errors. Useful when pushing many"
         " files concurrently.")
-    parser.add_option("--source", action="store_true", dest="push_source",
-        default=False, help="Force the pushing of the source file to the server")
+    parser.add_option("-s", "--source", action="store_true", dest="push_source",
+        default=False, help="Push the source file to the server.")
+
+    parser.add_option("-t", "--translations", action="store_true", dest="push_translations",
+        default=False, help="Push the translation files to the server")
     parser.add_option("--no-interactive", action="store_true", dest="no_interactive",
         default=False, help="Don't require user input when forcing a push.")
 
         if not r in available_resources:
             raise Exception("Specified resource '%s' does not exist." % r)
 
+    if not (options.push_source or options.push_translations):
+        parser.error("You need to specify at least on of the -s|--source,"
+            " -t|--translations flags with the push command.")
+
     prj.push(force=force_creation, resources=resources, languages=languages,
         skip=skip, source=options.push_source,
+        translations=options.push_translations,
         no_interactive=options.no_interactive)
+
     utils.MSG("Done.")
 
 def cmd_pull(argv, path_to_tx):

File txclib/project.py

                     fd.close()
 
 
-    def push(self, source=False, force=False, resources=[], languages=[],
+    def push(self, source=False, translations=False, force=False, resources=[], languages=[],
         skip=False, no_interactive=False):
         """
         Push all the resources
                             " --source option to create it." % resource)
                         continue
 
-            # Check if given language codes exist
-            if not languages:
-                languages = files.keys()
-            else:
-                f_langs = files.keys()
-                for l in languages:
-                    if l not in f_langs:
-                        languages.remove(l)
-                        ERRMSG("Warning: No mapping found for language code '%s'." %
-                            color_text(l,"RED"))
+            if translations:
+                # Check if given language codes exist
+                if not languages:
+                    languages = files.keys()
+                else:
+                    f_langs = files.keys()
+                    for l in languages:
+                        if l not in f_langs:
+                            languages.remove(l)
+                            ERRMSG("Warning: No mapping found for language code '%s'." %
+                                color_text(l,"RED"))
 
-            # Push translation files one by one
-            for lang in languages:
-                local_file = files[lang]
-                if languages and lang not in languages:
-                    continue
+                # Push translation files one by one
+                for lang in languages:
+                    local_file = files[lang]
+                    if languages and lang not in languages:
+                        continue
 
-                if not force:
+                    if not force:
+                        try:
+                            r = self.do_url_request('resource_stats',
+                                host=host,
+                                project=project_slug,
+                                resource=resource_slug,
+                                language=lang)
+
+                            # Check remote timestamp for file and skip update if needed
+                            stats = parse_json(r)
+                            time_format = "%Y-%m-%d %H:%M:%S"
+                            remote_time = time.mktime(datetime.datetime.strptime(stats[lang]['last_update'], time_format).utctimetuple())
+                        except Exception, e:
+                            remote_time = None
+                        local_time = time.mktime(time.gmtime(os.path.getmtime(local_file)))
+
+                        if remote_time and remote_time > local_time:
+                            MSG("Skipping '%s' translation (file: %s)." % (color_text(lang, "RED"), local_file))
+                            continue
+
+                    MSG("Pushing '%s' translations (file: %s)" % (color_text(lang, "RED"), local_file))
                     try:
-                        r = self.do_url_request('resource_stats',
+                        r = self.do_url_request('push_file', host=host, multipart=True,
+                            files=[( "%s;%s" % (resource_slug, lang),
+                            self.get_full_path(local_file))],
+                            method="POST",
+                            project=project_slug)
+                        r = parse_json(r)
+                        uuid = r['files'][0]['uuid']
+                        self.do_url_request('extract_translation',
                             host=host,
+                            data=compile_json({"uuid":uuid}),
+                            encoding='application/json',
+                            method="PUT",
                             project=project_slug,
                             resource=resource_slug,
                             language=lang)
-
-                        # Check remote timestamp for file and skip update if needed
-                        stats = parse_json(r)
-                        time_format = "%Y-%m-%d %H:%M:%S"
-                        remote_time = time.mktime(datetime.datetime.strptime(stats[lang]['last_update'], time_format).utctimetuple())
                     except Exception, e:
-                        remote_time = None
-                    local_time = time.mktime(time.gmtime(os.path.getmtime(local_file)))
-
-                    if remote_time and remote_time > local_time:
-                        MSG("Skipping '%s' translation (file: %s)." % (color_text(lang, "RED"), local_file))
-                        continue
-
-                MSG("Pushing '%s' translations (file: %s)" % (color_text(lang, "RED"), local_file))
-                try:
-                    r = self.do_url_request('push_file', host=host, multipart=True,
-                        files=[( "%s;%s" % (resource_slug, lang),
-                        self.get_full_path(local_file))],
-                        method="POST",
-                        project=project_slug)
-                    r = parse_json(r)
-                    uuid = r['files'][0]['uuid']
-                    self.do_url_request('extract_translation',
-                        host=host,
-                        data=compile_json({"uuid":uuid}),
-                        encoding='application/json',
-                        method="PUT",
-                        project=project_slug,
-                        resource=resource_slug,
-                        language=lang)
-                except Exception, e:
-                    if not skip:
-                        raise e
-                    else:
-                        ERRMSG(e)
+                        if not skip:
+                            raise e
+                        else:
+                            ERRMSG(e)
 
 
     def do_url_request(self, api_call, host=None, multipart=False, data=None,