Coverage for pyquickhelper/jenkinshelper/jenkins_helper.py: 97%
33 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-03 02:21 +0200
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-03 02:21 +0200
1"""
2@file
3@brief Helpers to prepare a local Jenkins server.
4"""
5import sys
6from ..loghelper import noLOG
9def get_platform(platform=None):
10 """
11 Returns *platform* if not *None*, ``sys.platform`` otherwise.
13 @param platform default values for which OS or
14 ``sys.platform``.
15 @return platform
17 This documentation was generated with a machine using the
18 following *OS* (among the
19 `possible values <https://docs.python.org/3/library/sys.html#sys.platform>`_).
21 .. runpython::
22 :showcode:
24 from pyquickhelper.jenkinshelper.jenkins_helper import get_platform
25 print(get_platform())
26 """
27 return platform or sys.platform
30def default_engines(platform=None):
31 """
32 Returns a dictionary with default values for Jenkins server,
33 you should update the path if the proposed path are not good.
35 @param platform default values for which OS or
36 ``get_platform(platform)``.
37 @return dictionary
39 .. warning::
41 Virtual environment with conda must be created on the same disk
42 as the original interpreter. The other scenario is not supported
43 by Anaconda.
45 It returns:
47 .. runpython::
49 from pyquickhelper.jenkinshelper import default_engines
50 print(default_engines())
51 """
52 platform = get_platform(platform)
53 if platform == "win32":
54 res = dict(Anaconda3="d:\\Anaconda3",
55 Python39="c:\\Python39_x64",
56 Python38="c:\\Python38_x64",
57 Python37="c:\\Python37_x64",
58 WinPython39="c:\\APythonENSAE\\python39",
59 WinPython38="c:\\APythonENSAE\\python38",
60 WinPython37="c:\\APythonENSAE\\python37")
61 elif platform == "linux":
62 res = dict(Anaconda3="/usr/local/miniconda3",
63 Python39="/usr/local/python39",
64 Python38="/usr/local/python38",
65 Python37="/usr/local/python37",
66 Python36="/usr/local/python36",
67 WinPython39="ERROR",
68 WinPython38="ERROR",
69 WinPython37="ERROR")
70 else:
71 raise ValueError( # pragma: no cover
72 f"Unknown value for platform '{platform}'.")
74 return res
77def default_jenkins_jobs(platform=None, github_owner="sdpython",
78 module_name="pyquickhelper"):
79 """
80 Example of a list of jobs for parameter *module*
81 of function @see fn setup_jenkins_server_yml.
83 @param platform platform
84 @param github_owner GitHub user
85 @param module_name module name or list of modules names
86 @return tuple
88 It returns:
90 .. runpython::
92 from pyquickhelper.jenkinshelper import default_jenkins_jobs
93 print(default_jenkins_jobs())
94 """
95 if ':' in module_name:
96 module_name, branch = module_name.split(':')
97 else:
98 branch = 'master'
99 platform = get_platform(platform)
100 plat = "win" if platform.startswith("win") else "lin"
101 pattern = "https://raw.githubusercontent.com/{1}/%s/{2}/.local.jenkins.{0}.yml".format(
102 plat, github_owner, branch)
103 yml = []
104 if not isinstance(module_name, list):
105 module_name = [module_name]
106 for i, c in enumerate(module_name):
107 yml.append(('yml', pattern % c, 'H H(5-6) * * %d' % (i % 7)))
108 return yml
111def setup_jenkins_server_yml(js, github="sdpython", modules=None,
112 overwrite=False, location=None, prefix="",
113 delete_first=False, disable_schedule=False,
114 fLOG=noLOG, **kwargs):
115 """
116 Sets up many jobs on :epkg:`Jenkins`.
118 @param js @see cl JenkinsExt, jenkins server
119 @param github github account if it does not start with *http://*,
120 the link to git repository of the project otherwise,
121 we assume the job comes from the same repository,
122 otherwise the function will have to called several times
123 @param modules modules for which to generate the Jenkins job (see @see fn default_jenkins_jobs)
124 @param overwrite do not create the job if it already exists
125 @param location None for default or a local folder
126 @param prefix add a prefix to the name
127 @param delete_first removes all jobs before adding new ones
128 @param disable_schedule disable scheduling for all jobs
129 @param fLOG logging function
130 @param kwargs see method @see me setup_jenkins_server
131 @return list of created jobs
133 Example::
135 from pyquickhelper.jenkinshelper (
136 import JenkinsExt, setup_jenkins_server_yml,
137 default_jenkins_jobs, default_engines)
139 user = "<user>"
140 password = "<password>"
141 modules = default_jenkins_jobs()
142 engines = default_engines()
143 js = JenkinsExt('http://localhost:8080/', user, password, engines=engines)
144 setup_jenkins_server_yml(js, github="sdpython", modules=modules, fLOG=print,
145 overwrite=True, delete_first=False,
146 location="d:\\\\jenkins\\\\pymy")
148 See `.local.jenkins.win.yml
149 <https://github.com/sdpython/pyquickhelper/blob/
150 master/.local.jenkins.win.yml>`_ (Windows) or
151 `.local.jenkins.lin.yml
152 <https://github.com/sdpython/pyquickhelper/blob/
153 master/.local.jenkins.lin.yml>`_ (Linux)
154 about the syntax of a :epkg:`yml` job description.
155 If *modules* is None, it is replaced by the results of
156 @see fn default_jenkins_jobs.
157 The platform is stored in *srv*.
158 """
159 if modules is None:
160 modules = default_jenkins_jobs(js.platform) # pragma: no cover
161 if delete_first:
162 js.delete_all_jobs() # pragma: no cover
163 r = js.setup_jenkins_server(
164 github=github, modules=modules, overwrite=overwrite,
165 location=location, prefix=prefix, disable_schedule=disable_schedule,
166 **kwargs)
167 return r
170def jenkins_final_postprocessing(xml_job, py27):
171 """
172 Postprocesses a job produced by :epkg:`Jenkins`.
174 @param xml_job :epkg:`xml` definition
175 @param py27 is it for :epkg:`Python` 27
176 @return new xml job
177 """
178 if py27:
179 # options are not allowed
180 xml_job = xml_job.replace(
181 "python -X faulthandler -X showrefcount", "python")
182 return xml_job