fix building of forked upstream packages
[ta/rpmbuilder.git] / stashworkspace.py
1 #! /usr/bin/python -tt
2 # Copyright 2019 Nokia
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 import argparse
17 import os
18 import subprocess
19
20 class Stasher(object):
21     def __init__(self, args=None):
22         self.args = args
23         self.backupfilename = "configuration.tar.gz"
24
25     def start(self):
26         self.push_workspace_to_remote(toserver=self.args.remotehost,
27                                       todirectory=self.args.remotedir,
28                                       workspace=self.args.workspace)
29
30     def push_workspace_to_remote(self, toserver, todirectory, workspace):
31         """ Move workspace backup to remote host """
32         destination = toserver + ":" + todirectory
33         sshoptions = 'ssh -o stricthostkeychecking=no -o userknownhostsfile=/dev/null -o batchmode=yes -o passwordauthentication=no'
34         sourceconfiguration = os.path.join(workspace, self.backupfilename)
35         sourcerpm = os.path.join(workspace, "buildrepository")
36         rsyncpathval = "mkdir -p " + todirectory + " && rsync"
37         cmd = ["/usr/bin/rsync",
38                "--verbose",
39                "--archive",
40                "--rsync-path", rsyncpathval,
41                "-e", sshoptions,
42                sourceconfiguration, sourcerpm,
43                destination]
44         try:
45             print subprocess.check_output(cmd, shell=False, stderr=subprocess.STDOUT)
46         except subprocess.CalledProcessError as err:
47             raise StasherError("Following command retured code %d: %s" % (err.returncode,
48                                                                           ' '.join(err.cmd)))
49
50 class StasherError(Exception):
51     """ Exceptions originating from builder """
52     pass
53
54
55
56 class ArgumentRemote(object):
57     """ Default arguments which are always needed """
58     def __init__(self):
59         """ Create parser for arguments """
60         self.parser = argparse.ArgumentParser(description='Workspace stasher copies workspace to remote host.')
61         self.set_arguments(self.parser)
62         self.parser.add_argument("--workspace",
63                                  help="Local (source) directory",
64                                  required=True)
65
66     def set_arguments(self, parser):
67         """ Add extra arguments to parser """
68         parser.add_argument("--remotehost",
69                             help="Remote host where script will ssh/rsync to store build",
70                             required=True)
71         parser.add_argument("--remotedir",
72                             help="Remote directory to use for storing build",
73                             required=True)
74
75
76 def main():
77     """ Get arguments required for stashing local workspace """
78     args = ArgumentRemote().parser.parse_args()
79
80     stasher = Stasher(args)
81     stasher.start()
82
83 if __name__ == "__main__":
84     main()