Coverage for /media/ldata/code/tendril/tendril/scripts/production.py: 0%

# Coverage for /media/ldata/code/tendril/tendril/scripts/production.py : 0%

## 200 statements 0 run 200 missing 0 excluded

Hot-keys on this page

r m x p toggle line displays

j k next/prev highlighted chunk

0 (zero) top of page

1 (one) first highlighted chunk

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 | # Copyright (C) 2015 Chintalagiri Shashank # # This file is part of Tendril. # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # 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 Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. """ This file is part of tendril See the COPYING, README, and INSTALL files for more information """ import yaml import os import tendril.boms.electronics import tendril.boms.outputbase import tendril.inventory.electronics import tendril.dox.production import tendril.dox.indent import tendril.dox.docstore import tendril.dox.labelmaker import tendril.gedaif.conffile from tendril.entityhub import projects from tendril.entityhub import serialnos from tendril.utils.pdf import merge_pdf from tendril.utils.terminal import TendrilProgressBar from tendril.utils.config import INSTANCE_ROOT from tendril.utils import log logger = log.get_logger(__name__, log.DEBUG) if __name__ == "__main__": bomlist = [] orderfolder = os.path.join(INSTANCE_ROOT, 'scratch', 'production') orderfile = os.path.join(orderfolder, 'order.yaml') with open(orderfile, 'r') as f: data = yaml.load(f) if os.path.exists(os.path.join(orderfolder, 'wsno')): with open(os.path.join(orderfolder, 'wsno'), 'r') as f: PROD_ORD_SNO = f.readline().strip() else: PROD_ORD_SNO = None snomap = {} if os.path.exists(os.path.join(orderfolder, 'snomap.yaml')): with open(os.path.join(orderfolder, 'snomap.yaml'), 'r') as f: snomap = yaml.load(f) if data['register'] is True: REGISTER = True else: REGISTER = False if data['halt_on_shortage'] is True: HALT_ON_SHORTAGE = True else: HALT_ON_SHORTAGE = False if data['include_refbom_for_no_am'] is True: INCLUDE_REFBOM_FOR_NO_AM = True else: INCLUDE_REFBOM_FOR_NO_AM = False if data['force_labels'] is True: FORCE_LABELS = True else: FORCE_LABELS = False if 'sourcing_orders' in data.keys(): SOURCING_ORDERS = data['sourcing_orders'] else: SOURCING_ORDERS = None if 'root_orders' in data.keys(): ROOT_ORDERS = data['root_orders'] if len(ROOT_ORDERS) > 1: logger.warning("Having more than one Root Order is not fully " "defined. This may break other functionality") else: ROOT_ORDERS = None if PROD_ORD_SNO is None: PROD_ORD_SNO = serialnos.get_serialno(series='PROD', efield=data['title'], register=REGISTER) # Generate Tendril Requisitions, confirm production viability. logger.info('Generating Card BOMs') for k, v in data['cards'].iteritems(): bom = tendril.boms.electronics.import_pcb(projects.cards[k]) obom = bom.create_output_bom(k) obom.multiply(v) logger.info('Inserting Card Bom : ' + obom.descriptor.configname + ' x' + str(obom.descriptor.multiplier)) bomlist.append(obom) cobom = tendril.boms.outputbase.CompositeOutputBom(bomlist) cobom.collapse_wires() with open(os.path.join(orderfolder, 'cobom.csv'), 'w') as f: logger.info('Exporting Composite Output BOM to File : ' + os.linesep + os.path.join(orderfolder, 'cobom.csv')) cobom.dump(f) unsourced = [] nlines = len(cobom.lines) pb = TendrilProgressBar(max=nlines) for pbidx, line in enumerate(cobom.lines): pb.next(note=line.ident) shortage = 0 logger.debug("Processing Line : " + line.ident) for idx, descriptor in enumerate(cobom.descriptors): logger.debug(" for earmark : " + descriptor.configname) earmark = descriptor.configname + \ ' x' + str(descriptor.multiplier) avail = tendril.inventory.electronics.get_total_availability(line.ident) # noqa if line.columns[idx] == 0: continue if avail > line.columns[idx]: tendril.inventory.electronics.reserve_items(line.ident, line.columns[idx], earmark) elif avail > 0: tendril.inventory.electronics.reserve_items(line.ident, avail, earmark) pshort = line.columns[idx] - avail shortage += pshort logger.debug('Adding Partial Qty of ' + line.ident + ' for ' + earmark + ' to shortage : ' + str(pshort)) else: shortage += line.columns[idx] logger.debug('Adding Full Qty of ' + line.ident + ' for ' + earmark + ' to shortage : ' + str(line.columns[idx])) if shortage > 0: unsourced.append((line.ident, shortage)) if len(unsourced) > 0: logger.warning("Shortage of the following components: ") for elem in unsourced: logger.warning("{0:<40}{1:>5}".format(elem[0], elem[1])) if HALT_ON_SHORTAGE is True: logger.info("Halt on shortage is set. Reversing changes " "and exiting") exit() # TODO Transfer Reservations # Generate Indent logger.info("Generating Indent") indentfolder = orderfolder if 'indentsno' in snomap.keys(): indentsno = snomap['indentsno'] else: indentsno = serialnos.get_serialno(series='IDT', efield='FOR ' + PROD_ORD_SNO, register=REGISTER) snomap['indentsno'] = indentsno title = data['title'] indentpath, indentsno = tendril.dox.indent.gen_stock_idt_from_cobom(orderfolder, # noqa indentsno, title, # noqa data['cards'], cobom) # noqa if REGISTER is True: tendril.dox.docstore.register_document(serialno=indentsno, docpath=indentpath, doctype='INVENTORY INDENT', efield=title) else: logger.info( "Not Registering Document : INVENTORY INDENT - " + indentsno ) # Generate Production Order logger.info("Generating Production Order") if REGISTER is True: tendril.dox.docstore.register_document(serialno=indentsno, docpath=os.path.join(orderfolder, 'cobom.csv'), # noqa doctype='PRODUCTION COBOM CSV', efield=title) serialnos.link_serialno(child=indentsno, parent=PROD_ORD_SNO) else: logger.info("Not registering used serial number : " + PROD_ORD_SNO) logger.info("Not Registering Document : PRODUCTION COBOM CSV - " + indentsno) logger.info("Not Linking Serial Nos : " + indentsno + ' to parent ' + PROD_ORD_SNO) snos = [] addldocs = [] manifests = [] manifestsfolder = os.path.join(orderfolder, 'manifests') if not os.path.exists(manifestsfolder): os.makedirs(manifestsfolder) manifestfiles = [] for card, qty in sorted(data['cards'].iteritems()): try: cardfolder = projects.cards[card] except KeyError: logger.error("Could not find Card in entityhub.cards") raise KeyError cardconf = tendril.gedaif.conffile.ConfigsFile(cardfolder) snoseries = cardconf.configdata['snoseries'] prodst = None lblst = None testst = None genmanifest = False if cardconf.configdata['documentation']['am'] is True: # Assembly manifest should be used prodst = "@AM" genmanifest = True elif cardconf.configdata['documentation']['am'] is False: # No Assembly manifest needed prodst = "@THIS" if INCLUDE_REFBOM_FOR_NO_AM is True: addldocs.append( os.path.join(cardconf.doc_folder, 'confdocs', card + '-bom.pdf') ) if cardconf.configdata['productionstrategy']['testing'] == 'normal': # Normal test procedure, Test when made testst = "@NOW" if cardconf.configdata['productionstrategy']['testing'] == 'lazy': # Lazy test procedure, Test when used testst = "@USE" if cardconf.configdata['productionstrategy']['labelling'] == 'normal': # Normal test procedure, Label when made lblst = "@NOW" if cardconf.configdata['productionstrategy']['testing'] == 'lazy': # Lazy test procedure, Label when used lblst = "@USE" series = cardconf.configdata['snoseries'] genlabel = False labels = [] if isinstance(cardconf.configdata['documentation']['label'], dict): for k in sorted(cardconf.configdata['documentation']['label'].keys()): # noqa labels.append( {'code': k, 'ident': card + '.' + cardconf.configdata['label'][k] } ) genlabel = True elif isinstance(cardconf.configdata['documentation']['label'], str): labels.append( {'code': cardconf.configdata['documentation']['label'], 'ident': card } ) genlabel = True for idx in range(qty): if card in snomap.keys(): if idx in snomap[card].keys(): sno = snomap[card][idx] else: sno = serialnos.get_serialno( series=series, efield=card, register=REGISTER ) snomap[card][idx] = sno else: snomap[card] = {} sno = serialnos.get_serialno( series=series, efield=card, register=REGISTER ) snomap[card][idx] = sno if REGISTER is True: serialnos.link_serialno(child=sno, parent=PROD_ORD_SNO) c = {'sno': sno, 'ident': card, 'prodst': prodst, 'lblst': lblst, 'testst': testst } snos.append(c) if genlabel is True: for label in labels: tendril.dox.labelmaker.manager.add_label( label['code'], label['ident'], sno ) if genmanifest is True: ampath = tendril.dox.production.gen_pcb_am(cardfolder, card, manifestsfolder, sno, # noqa productionorderno=PROD_ORD_SNO, # noqa indentsno=indentsno) # noqa manifestfiles.append(ampath) if REGISTER is True: tendril.dox.docstore.register_document(serialno=sno, docpath=ampath, # noqa doctype='ASSEMBLY MANIFEST') # noqa production_order = tendril.dox.production.gen_production_order(orderfolder, PROD_ORD_SNO, # noqa data, snos, # noqa sourcing_orders=SOURCING_ORDERS, # noqa root_orders=ROOT_ORDERS) # noqa labelpaths = tendril.dox.labelmaker.manager.generate_pdfs(orderfolder, force=FORCE_LABELS) # noqa if len(labelpaths) > 0: merge_pdf( labelpaths, os.path.join(orderfolder, 'device-labels.pdf') ) if len(addldocs) > 0: merge_pdf( [production_order] + addldocs, production_order, remove_sources=False ) if len(manifestfiles) > 0: merge_pdf( manifestfiles, os.path.join(orderfolder, 'manifests-printable.pdf') ) if REGISTER is True: if os.path.exists(os.path.join(orderfolder, 'device-labels.pdf')): tendril.dox.docstore.register_document(serialno=PROD_ORD_SNO, docpath=os.path.join(orderfolder, 'device-labels.pdf'), # noqa doctype='DEVICE LABELS', efield=data['title']) tendril.dox.docstore.register_document(serialno=PROD_ORD_SNO, docpath=production_order, doctype='PRODUCTION ORDER', efield=data['title']) tendril.dox.docstore.register_document(serialno=PROD_ORD_SNO, docpath=orderfile, doctype='PRODUCTION ORDER YAML', # noqa efield=data['title']) else: logger.info( "Not registering document : DEVICE LABELS " + PROD_ORD_SNO ) logger.info( "Not registering document : PRODUCTION ORDER " + PROD_ORD_SNO ) logger.info( "Not registering document : PRODUCTION ORDER YAML " + PROD_ORD_SNO ) with open(os.path.join(orderfolder, 'snomap.yaml'), 'w') as f: f.write(yaml.dump(snomap, default_flow_style=False)) if REGISTER is True: tendril.dox.docstore.register_document(serialno=PROD_ORD_SNO, docpath=os.path.join(orderfolder, 'snomap.yaml'), # noqa doctype='SNO MAP', efield=data['title'])