aboutsummaryrefslogtreecommitdiff
path: root/jupyter_files/Binhost_folder_structure.ipynb
blob: da2c7ed97bc0a9a9c104102f7c339e8a2f5650cc (plain)
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
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0b397cbb-8db3-40d9-a0a9-27ba0a774021",
   "metadata": {},
   "source": [
    "## Automatically creating new binhost directories when profile or gcc version changes"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "97c6e8be-a571-4fea-84c4-b226f03f56c6",
   "metadata": {},
   "source": [
    "On Gentoo Linux packages are compiled which is a time and energy consuming.\n",
    "To minimise both, I have a binhost to share compiled binaries between devices.\n",
    "The issues occurs when gcc major version changes - or a profile bump happens.\n",
    "If I do not notice either - problem. Binaries might not be compatible.\n",
    "So I wanted to automate a process of detection of those changes and automatically create subfolder for new binaries.\n",
    "When investigating I learned a neat trick. For the wise ones this is trivial, for me it was enligtening.\n",
    "Here it comes."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2a688afd-5f24-4eab-a1c7-119d372367b5",
   "metadata": {},
   "source": [
    "On a regular system, emerge resides here:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b1098db8-2f77-4b56-acef-f58113107dad",
   "metadata": {},
   "outputs": [],
   "source": [
    "$ which emerge\n",
    "/usr/bin/emerge"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9a1dcd6c-3f8c-455f-9d56-21e5157d0846",
   "metadata": {},
   "source": [
    "On my box with binhost:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bcb9918c-20fb-49fd-baaf-b3b63356de5a",
   "metadata": {},
   "outputs": [],
   "source": [
    "$ which emerge\n",
    "/usr/local/bin/emerge"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a84cf35c-e744-47c9-9500-d9b8c7d3bfef",
   "metadata": {},
   "source": [
    "And here is $PATH:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "94092561-ae6b-41eb-831a-71bffcf30e6e",
   "metadata": {},
   "outputs": [],
   "source": [
    "$ echo $PATH\n",
    "/usr/local/sbin:/usr/local/bin:/usr/bin:/opt/bin:/usr/lib/llvm/18/bin:/usr/lib/llvm/17/bin"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eb05de7d-f2f3-4cd3-acc2-769e096a3a5b",
   "metadata": {},
   "source": [
    "So we see that on binhost box, there is emerge that resides in location preceeding regular emerge - so it gets called first.\n",
    "It contains a shell script as follows:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b9f78265-a1de-47eb-be21-bb66c1ed3bed",
   "metadata": {},
   "outputs": [],
   "source": [
    "#!/usr/bin/env bash\n",
    "#\n",
    "# Script checking that binhost path exists before running emerge and\n",
    "# creating it if not exists\n",
    "\n",
    "PROFILE=$(eselect profile show|sed -n 2p|cut -d '/' -f4)\n",
    "GCC_VERSION=$(eselect gcc show|cut -d '-' -f5)\n",
    "BINHOST_DIR=\"/var/cache/binpkgs/${PROFILE}/gcc-${GCC_VERSION}.x/armv8a\"\n",
    "\n",
    "if [ ! -d \"${BINHOST_DIR}\" ]; then\n",
    "        echo \"${BINHOST_DIR} does not exist, creating\"\n",
    "        mkdir -p \"${BINHOST_DIR}\"\n",
    "else echo \"Binhost directory ${BINHOST_DIR} exists, proceeding with emerge ...\"\n",
    "fi\n",
    "\n",
    "PKGDIR=\"${BINHOST_DIR}\" /usr/bin/emerge \"$@\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d6d085ea-b68f-4220-adaf-00223393e58e",
   "metadata": {},
   "source": [
    "So on every profile change subfolder to /var/cache/binpkgs gets created,\n",
    "on every gcc major version change, subfolder to /var/cache/binpkgs/<profile> gets created.\n",
    "If folder already exists script only prints a message.\n",
    "Then it calls /usr/bin/emerge - the regular emerge - with $PKGDIR variable, so binary packages are saved to the correct directory. "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1d464645-b7ac-4cbc-b5cc-1bd4bd976f06",
   "metadata": {},
   "source": [
    "This is structure of /var/cache/binpkgs:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dee2b348-d16d-4709-a7cd-ac9424934009",
   "metadata": {},
   "outputs": [],
   "source": [
    "$ tree -L 3 /var/cache/binpkgs/\n",
    "/var/cache/binpkgs/\n",
    "├── 17.0\n",
    "│   └── gcc-13.x\n",
    "│       └── armv8a\n",
    "└── 23.0\n",
    "    ├── gcc-13.x\n",
    "    │   └── armv8a\n",
    "    └── gcc-14.x\n",
    "        └── armv8a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9f8f4a71-9fd1-4678-9cb0-a580c94f4582",
   "metadata": {},
   "source": [
    "One less thing to worry about."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}